package com.toutiao.score

import scala.collection.mutable.{HashMap => HMap}
import scala.math.{min,max,log}

import scala.collection.mutable.ArrayBuffer

// Title Match Feature

class MatchFeaturer {

	private
	def initDict(line:String, nn:Int) = {
	  val n = line.size-nn+1
	  val m = new HMap[String,Int]
	  for (i <- (0 until n)){
	    val v = line.slice(i, i+nn)
	    if (m.getOrElse(v, -1) == -1){
	      m(v) = i
	    }
	  }
	  m
	}
	
	def getCommonSegment(line1:String, line2:String, nn:Int = 2) = {
	  val m2 = initDict(line2,nn)
	  val n = line1.size-nn
	  val xs = new ArrayBuffer[(String,Int,Int)]
	  var i = 0
	  while (i <= n){
	    var v = line1.slice(i, i+nn)
	    if (m2.getOrElse(v,-1) != -1){
	      val z = m2(v)
	      var j = 0
	      var vv = line1.slice(i,i+nn+j)
	      var uu = line2.slice(z,z+nn+j)
	      while ( (uu == vv) && (i+nn+j < line1.size && z+nn+j < line2.size) ){
	        j += 1
	        vv = line1.slice(i,i+nn+j)
	        uu = line2.slice(z,z+nn+j)
	      }
	      if (vv != uu){
	        v = line1.slice(i,i+nn+j-1)
	      }
	      else {
	        v = vv
	      }
	      i += nn+j-1
	      xs.append((v,i,z))
	    }
	    else {
	      i += 1
	    }
	  }
	  
	  xs.toArray
	}
	
	def getProximitySim(xs:Array[(String,Int,Int)]) = {
	  val S = 0.6
	  val ps = new ArrayBuffer[Int]
	  ps.append(0)
	  val n = xs.size-1
	  for (i <- (0 until n)) {
	    val (v1,i1,j1) = xs(i)
	    val (v2,i2,j2) = xs(i+1)
	    val s1 = i2-i1-v1.size
	    val s2 = j2-j1-v1.size
	    val s = (v1.size+v2.size)/(v1.size+v2.size+min(s1,s2)+0.001) 
	    if (s > S){
	      ps.append(v1.size+v2.size)
	    }
	    else {
	      ps.append(v1.size)
	    }
	  }
	  
	  ps.max
	}
	
	def getSquareSim(line1:String, line2:String) = {
	  val p = """《.+》|\".+\"|“.+”""".r
	  
	  val s1 = p.findAllMatchIn(line1).map(_.toString).toSet
	  val s2 = p.findAllMatchIn(line2).map(_.toString).toSet
	  
	  (s1 & s2).toArray
	}
	
	def getDigitSim(line1:String,line2:String) = {
	  val p = """(\d+\.\d+|\d+)""".r
	  val s1 = p.findAllMatchIn(line1).map(_.toString).toSet
	  val s2 = p.findAllMatchIn(line2).map(_.toString).toSet
	  
	  val xs = s1 & s2
	  xs.filter(_.size > 1).toArray
	}
}

//object SimFeatureTester extends App {
//  
//  val line1 = "美国女子天津偷师回国卖煎饼果子1个36元"
//  val line2 = "美国女子回国卖煎饼变土豪煎饼卖出36元高价“国粹”被窃取的节奏"
//    
////  val xs = SimFeaturer.getCommonSegment(line1,line2,nn=4)
////  
////  val s = SimFeaturer.getProximitySim(xs)
////  
////  println(s)
//
////	val line1 = "《1》梦萍嫁豪门近照曝光"
////	val line2 = "《1》里那个骄纵的梦萍现在怎么样了？"  
////    val xs = SimFeaturer.getSquareSim(line1, line2)
////    
////    println(xs.toBuffer)
//    
//    val xs = SimFeaturer.getDigitSim(line1, line2)
//    
//    println(xs.toBuffer)
//  
//}

